package com.study.base.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: NioFileChannel02
 * @date: 2021/1/21 13:33
 * @author: hcm
 * @version: 1.0
 */
public class NioFileChannel02 {
    public static void main(String[] args) throws IOException {
        //创建文件的输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //通过fileInputStream获取对应的FileChanel
        FileChannel fileChannel = fileInputStream.getChannel();

        //创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        //将合同到数据读取到buffer
        fileChannel.read(byteBuffer);

        //将字节数据转换成字符串打印
        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();

    }
}
